<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="dialogVisible=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination layout="prev,pager,next" :total="total" :page-size="pageSetting.pagesize" @current-change="currChange" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
      <el-dialog
        title="新增角色"
        :visible="dialogVisible"
        width="30%"
      >
        <el-form ref="addForm" :model="formData" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item></el-form>
        <template #footer>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="brnCancel">取消</el-button>
        </template>

      </el-dialog>
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <el-checkbox-group v-model="checkPerm">
          <el-tree
            ref="permTree"
            :expand-on-click-node="false"
            :data="permData"
            :props="{label:'name'}"
            default-expand-all
          >
            <template #default="{data}">
              <el-checkbox :label="data.id">  {{ data.name }}</el-checkbox>
            </template>
          </el-tree></el-checkbox-group>

        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, delRole, addRole, getRoleDetail, editRole, editAssginPerm } from '@/api/setting'
import { listToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
export default {
  data() {
    return {
      roleId: '',
      checkPerm: [],
      permData: [],
      showPermDialog: false,
      companyInfo: {},
      roleList: [],
      pageSetting: {
        page: 1, pagesize: 2
      },
      total: 0,
      dialogVisible: false,
      formData: { name: '', description: '' },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }}
  },
  async created() {
    this.getCompanyInfo()
    this.getRoleList()
    this.permData = listToTreeData(await getPermissionList(), '0')
  },
  methods: {
    async  btnPermOK() {
      await editAssginPerm({ permIds: this.checkPerm, id: this.roleId })
      this.$message.success('编辑ok')
      this.showPermDialog = false
    },
    btnPermCancel() {
      this.checkPerm = []
      this.showPermDialog = false
    },
    async assignPerm(id) {
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.checkPerm = permIds
      this.showPermDialog = true
    },

    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(1)
    },
    async getRoleList() {
      const { total, rows } = await getRoleList(this.pageSetting)
      this.roleList = rows
      this.total = total
    },
    currChange(newPage) {
      this.pageSetting.page = newPage
      this.getRoleList()
    },
    async delRole(id) {
      console.log(id)
      await this.$confirm('是否要删除？')
      await delRole(id)
      this.$message.success('删除ok')
      this.getRoleList()
    },
    async btnOK() {
      await this.$refs.addForm.validate()
      if (this.formData.id) {
        await editRole(this.formData)
      } else {
        await addRole(this.formData)
      }
      this.$message.success('操作ok')
      this.getRoleList()
      this.$refs.addForm.resetFields()
      this.dialogVisible = false
    },
    brnCancel() {
      this.formData = { name: '', description: '' }
      this.$refs.addForm.resetFields()
      this.dialogVisible = false
    },
    async editRole(id) {
      this.formData = await getRoleDetail(id)
      this.dialogVisible = true
    }
  }

}
</script>

<style>

</style>
